/************************************************************************************/
/*  Program name CAPFAIMP_TXF_DETAILS.sql                                  			*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 23 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_TXF_DETAILS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_TXF_DETAILS;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_TRANSFER_DETAILS
			VALUES
			(
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.TRANSFER_HEADER_ID),
			CAPBRIMP_MAPPING.GetFADistIDMapping(C1.DISTRIBUTION_ID),
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.BOOK_HEADER_ID)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/